<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="线路标识" prop="lineId">
        <el-select v-model="queryParams.lineId" placeholder="请选择线路标识" clearable>
          <el-option
              v-for="dict in line_info"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="光纤通道" prop="fiberNum">
        <el-select v-model="queryParams.fiberNum" placeholder="请选择光纤通道编号" clearable>
          <el-option
              v-for="dict in fiber_info"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['warn:warnSectionInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['warn:warnSectionInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['warn:warnSectionInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['warn:warnSectionInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="warnSectionInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流水号" align="center" prop="id" v-if="false"/>
      <el-table-column label="线路标识" align="center" prop="lineId">
        <template #default="scope">
          <dict-tag :options="line_info" :value="scope.row.lineId"/>
        </template>
      </el-table-column>
      <el-table-column label="光纤通道编号" align="center" prop="fiberNum">
        <template #default="scope">
          <dict-tag :options="fiber_info" :value="scope.row.fiberNum"/>
        </template>
      </el-table-column>
      <el-table-column label="分段区域的编号" align="center" prop="sectionNo" />
      <el-table-column label="分段区域的名称" align="center" prop="sectionName" />
      <el-table-column label="起点的光纤距离" align="center" prop="startPoint" width="120px"/>
      <el-table-column label="终点的光纤距离" align="center" prop="endPoint" width="120px"/>
      <el-table-column label="起点距离(m)" align="center" prop="startDistance" width="120px"/>
      <el-table-column label="终点距离(m)" align="center" prop="endDistance" width="120px"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['warn:warnSectionInfo:edit']">修改</el-button>
<!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['warn:warnSectionInfo:remove']">删除</el-button>-->
          <el-button link type="primary" icon="Warning" @click="handleWarnRelative(scope.row)" v-hasPermi="['warn:warnSectionInfo:remove']">相对预警</el-button>
<!--          <el-button link type="primary" icon="Warning" @click="handleDelete(scope.row)" v-hasPermi="['warn:warnSectionInfo:remove']">绝对预警</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改预警分段信息对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="warnSectionInfoRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="线路标识" prop="lineId">
          <el-select v-model="form.lineId" placeholder="请选择线路标识">
            <el-option
                v-for="dict in line_info"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="光纤通道编号" prop="fiberNum">
          <el-select v-model="form.fiberNum" placeholder="请选择光纤通道编号">
            <el-option
                v-for="dict in fiber_info"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分段区域的编号" prop="sectionNo">
          <el-input v-model="form.sectionNo" placeholder="请输入分段区域的编号" />
        </el-form-item>
        <el-form-item label="分段区域的名称" prop="sectionName">
          <el-input v-model="form.sectionName" placeholder="请输入分段区域的名称" />
        </el-form-item>
        <el-form-item label="起点的光纤距离" prop="startPoint">
          <el-input v-model="form.startPoint" placeholder="请输入起点的光纤距离" />
        </el-form-item>
        <el-form-item label="终点的光纤距离" prop="endPoint">
          <el-input v-model="form.endPoint" placeholder="请输入终点的光纤距离" />
        </el-form-item>
        <el-form-item label="起点距离(m)" prop="startDistance">
          <el-input v-model="form.startDistance" placeholder="请输入起点距离(m)" />
        </el-form-item>
        <el-form-item label="终点距离(m)" prop="endDistance">
          <el-input v-model="form.endDistance" placeholder="请输入终点距离(m)" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog :title="warnTitle" v-model="warnOpen" width="600px" :fullscreen="true"
               :close-on-press-escape="false" append-to-body>
            <warn-relative ref="warnRelativeRef" :warnSectionInfo="selectedSectionInfo"></warn-relative>
    </el-dialog>
  </div>
</template>

<script setup name="WarnSectionInfo">
import { listWarnSectionInfo, getWarnSectionInfo, delWarnSectionInfo, addWarnSectionInfo, updateWarnSectionInfo } from "@/api/warn/warnSectionInfo";
import WarnRelative from "@/views/warn/sectionInfo/warnRelative.vue"
import * as echarts from "echarts";
import {onMounted} from "vue";
const { proxy } = getCurrentInstance();
const { line_info, fiber_info } = proxy.useDict('line_info', 'fiber_info');

const warnSectionInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const warnTitle = ref("")
const warnOpen = ref(false)
const selectedSectionInfo = ref({})
const warnRelativeRef = ref(null)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    lineId: null,
    fiberNum: null,
  },
  rules: {
    lineId: [
      { required: true, message: "线路标识不能为空", trigger: "change" }
    ],
    fiberNum: [
      { required: true, message: "光纤通道编号不能为空", trigger: "change" }
    ],
    sectionNo: [
      { required: true, message: "分段区域的编号不能为空", trigger: "blur" }
    ],
    sectionName: [
      { required: true, message: "分段区域的名称不能为空", trigger: "blur" }
    ],
    startDistance: [
      { required: true, message: "起点距离(m)不能为空", trigger: "blur" }
    ],
    endDistance: [
      { required: true, message: "终点距离(m)不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询预警分段信息列表 */
function getList() {
  loading.value = true;
  listWarnSectionInfo(queryParams.value).then(response => {
    warnSectionInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    lineId: null,
    fiberNum: null,
    sectionNo: null,
    sectionName: null,
    startPoint: null,
    endPoint: null,
    startDistance: null,
    endDistance: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("warnSectionInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function handleWarnRelative(row){
  if (!row){
    return;
  }
  console.log(row)
  selectedSectionInfo.value = row
  warnTitle.value = row.fiberNum + "/" + row.sectionName+"/相对预警的配置"
  warnOpen.value = true;
  // proxy.$refs["warnRelativeRef"].value.init()

  // console.log(proxy.$ref["warnRelativeRef"])
  // 新建一个promise对象
  let newPromise = new Promise((resolve) => {
    resolve()
  })
  //然后异步执行echarts的初始化函数
  newPromise.then(() => {
    // console.log(warnRelativeRef)
  //   console.log(proxy.$ref["warnRelativeRef"])
    warnRelativeRef.value.init()
  })
}


// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加预警分段信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getWarnSectionInfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改预警分段信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["warnSectionInfoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateWarnSectionInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addWarnSectionInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除预警分段信息编号为"' + _ids + '"的数据项？').then(function() {
    return delWarnSectionInfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('warn/warnSectionInfo/export', {
    ...queryParams.value
  }, `warnSectionInfo_${new Date().getTime()}.xlsx`)
}

onMounted(()=>{
  // console.log("---------------------- 111 ------------------------")
  // console.log(warnRelativeRef)
  // console.log("---------------------- 222 ------------------------")
})

getList();
</script>
